from pyxll import xl_func
from RIKBClass import RIKBBond
from scipy.stats import norm
from math import log
from math import sqrt
from math import exp

@xl_func("string name: string")
def hello(name):
    """return a familiar greeting"""
    return "Hello, %s" %name

@xl_func("int genre, float ytm, datetime settlementDay: float")
def cleanPrice(genre,ytm,settlementDay):
    a = RIKBBond(genre, settlementDay)
    return a.cleanPrice(ytm)

@xl_func("int genre, float ytm, datetime settlementDay: float")
def dirtyPrice(genre,ytm,settlementDay):
    a = RIKBBond(genre, settlementDay)
    return a.dirtyPrice(ytm)

@xl_func("int genre, float price, float rate, datetime expiryDay, datetime settlementDay: float")
def fwdPrice(genre,price,rate,expiryDay,settlementDay):
    a = RIKBBond(genre, settlementDay)
    return a.fwdPrice(price,rate,expiryDay)

@xl_func("string Kind, float Price, float Strike, float Rate, float Time, float Volatility, float Yield: float")
def blackscholes(Kind, Price, Strike, Rate, Time, Volatility, Yield):
    d1 = float((log(Price/Strike)+(Rate - Yield + Volatility**2/2)*Time)/(Volatility*sqrt(Time)))
    d2 = float(d1 - Volatility*sqrt(Time))

    if Kind.capitalize() == 'C':
        premia = Price*exp(-Yield*Time)*norm.cdf(d1)-Strike*exp(-Rate*Time)*norm.cdf(d2)
    elif Kind.capitalize() == 'P':
        premia = Strike*exp(-Rate*Time)*norm.cdf(-d2)-Price*exp(-Yield*Time)*norm.cdf(-d1)
    else:
          print "Fuck you"
    return premia

@xl_func("string Kind, float Price, float Strike, float Rate, float Time, float Volatility, float Yield: float")
def blackscholesdelta(Kind, Price, Strike, Rate, Time, Volatility, Yield):
    d1 = float((log(Price/Strike)+(Rate - Yield + Volatility**2/2)*Time)/(Volatility*sqrt(Time)))
    d2 = float(d1 - Volatility*sqrt(Time))

    if Kind.capitalize() == 'C':
        delta = exp(-Yield*Time)*norm.cdf(d1)
    elif Kind.capitalize() == 'P':
        delta = exp(-Yield*Time)*(norm.cdf(d1)-1)
    else:
          print "Fuck you"
    return delta

@xl_func("float Price, float Strike, float Rate, float Time, float Volatility, float Yield: float")
def blackscholesgamma(Price, Strike, Rate, Time, Volatility, Yield):
    d1 = float((log(Price/Strike)+(Rate - Yield + Volatility**2/2)*Time)/(Volatility*sqrt(Time)))

    return norm.cdf(d1)*exp(-Yield*Time)/(Price*Volatility*sqrt(Time))

@xl_func("float Price, float Strike, float Rate, float Time, float Volatility, float Yield: float")
def blackscholesvega(Price, Strike, Rate, Time, Volatility, Yield):
    d1 = float((log(Price/Strike)+(Rate - Yield + Volatility**2/2)*Time)/(Volatility*sqrt(Time)))

    return Price*exp(-Yield*Time)*norm.cdf(d1)*sqrt(Time)

@xl_func("float Price, float Strike, float Rate, float Time, float Volatility, float Yield: float")
def blackscholesd1(Price, Strike, Rate, Time, Volatility, Yield):
    d1 = float((log(Price/Strike)+(Rate - Yield + Volatility**2/2)*Time)/(Volatility*sqrt(Time)))
    d2 = float(d1 - Volatility*sqrt(Time))

    return d1

@xl_func("float Price, float Strike, float Rate, float Time, float Volatility, float Yield: float")
def blackscholesd2(Price, Strike, Rate, Time, Volatility, Yield):
    d1 = float((log(Price/Strike)+(Rate - Yield + Volatility**2/2)*Time)/(Volatility*sqrt(Time)))
    d2 = float(d1 - Volatility*sqrt(Time))


    return d2
